package ui.swing.event;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ui.swing.Console;

public class TrackEvent extends JApplet {

	private Map<String, JTextField> h = new HashMap<String, JTextField>();

	private String[] events = { "focusGained", "focusLost", "keyPressed",
			"keyReleased", "keyTyped", "mouseClicked", "mousePressed",
			"mouseReleased", "mouseEntered", "mouseExited", "mouseDragged",
			"mouseMoved" };

	private MyButton b1 = new MyButton(Color.BLUE, "test1");
	private MyButton b2 = new MyButton(Color.RED, "test2");

	public void init() {
		Container c = getContentPane();
		c.setLayout(new BorderLayout());

		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
		mainPanel.add(Box.createVerticalStrut(5));
		for (int i = 0; i < events.length; i++) {
			Box hBox = Box.createHorizontalBox();
			hBox.setPreferredSize(new Dimension(800, 20));
			JLabel lbl = new JLabel(events[i], JLabel.RIGHT);
			lbl.setPreferredSize(new Dimension(120, 20));
			lbl.setAlignmentY(0.0f);
			// lbl.setBorder(new LineBorder(Color.BLACK,1,true));
			hBox.add(lbl);

			hBox.add(Box.createHorizontalStrut(10));

			JTextField t = new JTextField();
			t.setPreferredSize(new Dimension(120, 20));
			// t.setEditable(false);
			hBox.add(t);

			mainPanel.add(hBox);
			h.put(events[i], t);
			mainPanel.add(Box.createVerticalStrut(5));
		}

		JPanel buttonPanel = new JPanel(new GridLayout(1, 2));
		buttonPanel.add(b1);
		buttonPanel.add(b2);

		c.add(mainPanel, BorderLayout.CENTER);
		c.add(buttonPanel, BorderLayout.SOUTH);
	}

	class MyButton extends JButton {

		public MyButton(Color color, String name) {
			super(name);
			setBackground(color);
			addFocusListener(f1);
			addKeyListener(kl);
			addMouseListener(ml);
			addMouseMotionListener(mml);
		}

		FocusListener f1 = new FocusListener() {

			public void focusGained(FocusEvent e) {
				report("focusGained", e.paramString());
			}

			public void focusLost(FocusEvent e) {
				report("focusLost", e.paramString());
			}

		};

		KeyListener kl = new KeyListener() {
			public void keyPressed(KeyEvent e) {
				report("keyPressed", e.paramString());
			}

			public void keyReleased(KeyEvent e) {
				report("keyReleased", e.paramString());
			}

			public void keyTyped(KeyEvent e) {
				report("keyTyped", e.paramString());
			}
		};

		MouseListener ml = new MouseListener() {
			public void mouseClicked(MouseEvent e) {
				report("mouseClicked", e.paramString());
			}

			public void mouseEntered(MouseEvent e) {
				report("mouseEntered", e.paramString());
			}

			public void mouseExited(MouseEvent e) {
				report("mouseExited", e.paramString());
			}

			public void mousePressed(MouseEvent e) {
				report("mousePressed", e.paramString());
			}

			public void mouseReleased(MouseEvent e) {
				report("mouseReleased", e.paramString());
			}
		};

		MouseMotionListener mml = new MouseMotionListener() {
			public void mouseDragged(MouseEvent e) {
				report("mouseDragged", e.paramString());
			}

			public void mouseMoved(MouseEvent e) {
				report("mouseMoved", e.paramString());
			}
		};

		void report(String field, String msg) {
			h.get(field).setText(msg);
		}
	}

	public static void main(String[] args) {
		Console.run(new TrackEvent(), 800, 500);
	}
}
